<template>
  <!--<div class="d1">-->
  <el-form :model="textForm" status-icon :rules="textRuels" ref="textForm" label-width="100px" class="demo-textForm">
    <el-form-item hidden="hidden">
      <el-input v-model="textForm.userId">
        <i class="el-icon-edit el-input__icon" slot="suffix"></i>
      </el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="userNickname">
      <el-input placeholder="请输入内容" name="userNickname" v-model="textForm.userNickname">
        <i class="el-icon-edit el-input__icon" slot="suffix"></i>
      </el-input>
    </el-form-item>
    <el-form-item label="简介">
      <el-input type="textarea" placeholder="请输入内容" v-model="textForm.userSuggest"
                maxlength="300" show-word-limit>
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('textForm')">提交</el-button>
      <el-button>重置</el-button>
    </el-form-item>
  </el-form>
  <!--</div>-->
</template>

<script>
import {Revamp, UserGetUserNickname} from "../api/user/user";

export default {
  name: "Message",
  data() {
    //验证手机号
    var UserNicknameBlor = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('昵称不能为空！！'));
      }
      UserGetUserNickname(this.textForm.userId, this.textForm.userNickname).then(result => {
        console.log(result.data);
        if (!result.data.toString()) {
          console.log("可以使用");
          callback();
        } else {
          return callback(new Error('改昵称已被使用！'));
        }
      })
    };
    return {
      textForm: {
        userId: '',
        userNickname: '',
        userSuggest: '',
      },
      textRuels: {
        userNickname: [
          {validator: UserNicknameBlor, trigger: "blur"},
        ]
      }
    }
  }, created() {
    var _this = this;
    var user = JSON.parse(sessionStorage.getItem('user'));
    if (user) {
      _this.textForm.userId = user.userId;
      _this.userTel = user.userTel;
      _this.textForm.userNickname = user.userNickname;
      _this.textForm.userSuggest = user.userSuggest;
    }
  }, methods: {
    revamp() {
      var th = this;
      if (this.textForm.userId != null && this.textForm.userId != '') {
        Revamp(th.textForm).then(result => {
          if (result != 0) {
            var user = JSON.parse(sessionStorage.getItem('user'));
            user.userNickname = this.textForm.userNickname;
            user.userSuggest = this.textForm.userSuggest;
            sessionStorage.setItem('user', JSON.stringify(user));
            this.$router.go(0);
          } else {
            this.$message.error("提交失败，请重新提交！")
          }
        })
      } else {
        this.$message.error("提交失败")
      }

    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("ok")
          this.revamp();
          // this.$http.post("")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>
.d1 {
  width: 400px;
  height: 200px;
  border: 1px solid gold;
}

.demo-textForm {
  width: 400px;
  height: 200px;
}
</style>
